<template>
  <div class="login-container">
    <div class="login-box">
      <div class="login-left">
        <div class="company-info">
          <img src="../assets/logo/logo.png" alt="Logo" class="logo-image">
          <h2>金睿智后台管理系统</h2>
          <p class="slogan">高效 · 专业 · 可靠</p>
        </div>
      </div>
      <div class="login-right">
        <div class="login-form-container">
          <h3 class="welcome-title">欢迎登录</h3>
          <p class="welcome-subtitle">金睿智后台管理系统</p>
          
          <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
            <el-form-item prop="username">
              <el-input
                v-model="loginForm.username"
                type="text"
                auto-complete="off"
                placeholder="请输入账号"
              >
                <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />
              </el-input>
            </el-form-item>
            
            <el-form-item prop="password">
              <el-input
                v-model="loginForm.password"
                type="password"
                auto-complete="off"
                placeholder="请输入密码"
                @keyup.enter.native="handleLogin"
              >
                <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
              </el-input>
            </el-form-item>
            
            <el-form-item prop="code" v-if="captchaEnabled">
              <el-input
                v-model="loginForm.code"
                auto-complete="off"
                placeholder="验证码"
                style="width: 65%"
                @keyup.enter.native="handleLogin"
              >
                <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />
              </el-input>
              <div class="login-code">
                <img :src="codeUrl" @click="getCode" class="login-code-img" alt="验证码"/>
              </div>
            </el-form-item>
            
            <div class="remember-me">
              <el-checkbox v-model="loginForm.rememberMe">记住密码</el-checkbox>
            </div>
            
            <el-form-item style="width:100%;">
              <el-button
                :loading="loading"
                size="medium"
                type="primary"
                class="login-button"
                @click.native.prevent="handleLogin"
              >
                <span v-if="!loading">登 录</span>
                <span v-else>登 录 中...</span>
              </el-button>
              
              <div class="additional-links" v-if="register">
                <router-link class="register-link" :to="'/register'">立即注册</router-link>
              </div>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </div>
    
    <div class="login-footer">
      <span>备案号： 冀ICP备2024075345号</span>
    </div>
  </div>
</template>

<script>
import { getCodeImg } from "@/api/login";
import Cookies from "js-cookie";
import { encrypt, decrypt } from '@/utils/jsencrypt'

export default {
  name: "Login",
  data() {
    return {
      codeUrl: "",
      loginForm: {
        username: "admin",
        password: "admin123",
        rememberMe: false,
        code: "",
        uuid: ""
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "请输入您的账号" }
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入您的密码" }
        ],
        code: [{ required: true, trigger: "change", message: "请输入验证码" }]
      },
      loading: false,
      // 验证码开关
      captchaEnabled: true,
      // 注册开关
      register: false,
      redirect: undefined
    };
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  },
  created() {
    this.getCode();
    this.getCookie();
  },
  methods: {
    getCode() {
      getCodeImg().then(res => {
        this.captchaEnabled = res.captchaEnabled === undefined ? true : res.captchaEnabled;
        if (this.captchaEnabled) {
          this.codeUrl = "data:image/gif;base64," + res.img;
          this.loginForm.uuid = res.uuid;
        }
      });
    },
    getCookie() {
      const username = Cookies.get("username");
      const password = Cookies.get("password");
      const rememberMe = Cookies.get('rememberMe')
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password: password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
      };
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true;
          if (this.loginForm.rememberMe) {
            Cookies.set("username", this.loginForm.username, { expires: 30 });
            Cookies.set("password", encrypt(this.loginForm.password), { expires: 30 });
            Cookies.set('rememberMe', this.loginForm.rememberMe, { expires: 30 });
          } else {
            Cookies.remove("username");
            Cookies.remove("password");
            Cookies.remove('rememberMe');
          }
          this.$store.dispatch("Login", this.loginForm).then(() => {
            this.$router.push({ path: this.redirect || "/" }).catch(()=>{});
          }).catch(() => {
            this.loading = false;
            if (this.captchaEnabled) {
              this.getCode();
            }
          });
        }
      });
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss">
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  width: 100%;
  background: linear-gradient(135deg, #24243e, #302b63, #0f0c29);
  position: relative;
  overflow: hidden;
  
  &::before {
    content: "";
    position: absolute;
    width: 100%;
    height: 100%;
    background: radial-gradient(circle, rgba(255,255,255,0.1) 0%, rgba(255,255,255,0) 70%);
    z-index: 0;
  }
}

.login-box {
  display: flex;
  width: 900px;
  height: 560px;
  border-radius: 10px;
  overflow: hidden;
  box-shadow: 0 15px 30px rgba(0, 0, 0, 0.3);
  z-index: 10;
  background-color: #fff;
}

.login-left {
  width: 40%;
  background: linear-gradient(135deg, #3949ab, #1a237e);
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  
  &::before {
    content: "";
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-image: url("data:image/svg+xml,%3Csvg width='100' height='100' viewBox='0 0 100 100' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M11 18c3.866 0 7-3.134 7-7s-3.134-7-7-7-7 3.134-7 7 3.134 7 7 7zm48 25c3.866 0 7-3.134 7-7s-3.134-7-7-7-7 3.134-7 7 3.134 7 7 7zm-43-7c1.657 0 3-1.343 3-3s-1.343-3-3-3-3 1.343-3 3 1.343 3 3 3zm63 31c1.657 0 3-1.343 3-3s-1.343-3-3-3-3 1.343-3 3 1.343 3 3 3zM34 90c1.657 0 3-1.343 3-3s-1.343-3-3-3-3 1.343-3 3 1.343 3 3 3zm56-76c1.657 0 3-1.343 3-3s-1.343-3-3-3-3 1.343-3 3 1.343 3 3 3zM12 86c2.21 0 4-1.79 4-4s-1.79-4-4-4-4 1.79-4 4 1.79 4 4 4zm28-65c2.21 0 4-1.79 4-4s-1.79-4-4-4-4 1.79-4 4 1.79 4 4 4zm23-11c2.76 0 5-2.24 5-5s-2.24-5-5-5-5 2.24-5 5 2.24 5 5 5zm-6 60c2.21 0 4-1.79 4-4s-1.79-4-4-4-4 1.79-4 4 1.79 4 4 4zm29 22c2.76 0 5-2.24 5-5s-2.24-5-5-5-5 2.24-5 5 2.24 5 5 5zM32 63c2.76 0 5-2.24 5-5s-2.24-5-5-5-5 2.24-5 5 2.24 5 5 5zm57-13c2.76 0 5-2.24 5-5s-2.24-5-5-5-5 2.24-5 5 2.24 5 5 5zm-9-21c1.105 0 2-.895 2-2s-.895-2-2-2-2 .895-2 2 .895 2 2 2zM60 91c1.105 0 2-.895 2-2s-.895-2-2-2-2 .895-2 2 .895 2 2 2zM35 41c1.105 0 2-.895 2-2s-.895-2-2-2-2 .895-2 2 .895 2 2 2zM12 60c1.105 0 2-.895 2-2s-.895-2-2-2-2 .895-2 2 .895 2 2 2z' fill='%23ffffff' fill-opacity='0.1' fill-rule='evenodd'/%3E%3C/svg%3E");
    opacity: 0.5;
  }
}

.company-info {
  text-align: center;
  color: #fff;
  padding: 0 20px;
  
  .logo-image {
    width: 120px;
    height: 120px;
    margin-bottom: 20px;
    object-fit: contain;
  }
  
  h2 {
    font-size: 26px;
    margin-bottom: 15px;
    font-weight: 600;
    letter-spacing: 1px;
  }
  
  .slogan {
    font-size: 16px;
    font-weight: 300;
    margin-top: 20px;
    letter-spacing: 2px;
  }
}

.login-right {
  width: 60%;
  padding: 40px;
  display: flex;
  align-items: center;
}

.login-form-container {
  width: 100%;
  max-width: 400px;
  margin: 0 auto;
}

.welcome-title {
  font-size: 28px;
  color: #333;
  margin-bottom: 8px;
  font-weight: 600;
}

.welcome-subtitle {
  color: #666;
  font-size: 16px;
  margin-bottom: 30px;
}

.login-form {
  .el-form-item {
    margin-bottom: 25px;
  }
  
  .el-input {
    .el-input__inner {
      height: 46px;
      line-height: 46px;
      border-radius: 4px;
      border: 1px solid #dcdfe6;
      background-color: #f5f7fa;
      padding-left: 42px;
      transition: all 0.3s;
      
      &:focus, &:hover {
        border-color: #3949ab;
        background-color: #fff;
      }
    }
    
    .input-icon {
      color: #889aa4;
      font-size: 16px;
      height: 46px;
      width: 16px;
      margin: 0 13px;
      display: flex;
      align-items: center;
      justify-content: center;
    }
  }
}

.remember-me {
  margin-bottom: 25px;
  color: #606266;
  font-size: 14px;
}

.login-button {
  width: 100%;
  height: 46px;
  border-radius: 4px;
  margin-bottom: 15px;
  background: linear-gradient(90deg, #3949ab, #283593);
  border: none;
  font-size: 16px;
  font-weight: 500;
  letter-spacing: 1px;
  
  &:hover, &:focus {
    background: linear-gradient(90deg, #283593, #1a237e);
  }

}

.additional-links {
  text-align: center;
  margin-top: 15px;
  
  .register-link {
    color: #3949ab;
    font-size: 14px;
    text-decoration: none;
    
    &:hover {
      text-decoration: underline;
    }
  }
}

.login-code {
  float: right;
  width: 33%;
  height: 46px;
  
  .login-code-img {
    height: 46px;
    border-radius: 4px;
    cursor: pointer;
  }
}

.login-footer {
  position: absolute;
  bottom: 15px;
  width: 100%;
  text-align: center;
  color: rgba(255, 255, 255, 0.7);
  font-size: 12px;
  letter-spacing: 1px;
  z-index: 10;
}
</style>

